package com.ruihu.admin.module.business.tasknode.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruihu.admin.common.domain.ResponseDTO;
import com.ruihu.admin.module.business.tasknode.dao.NodeDefectRelationDao;
import com.ruihu.admin.module.business.tasknode.domain.dto.NodeDefectRelationAddDTO;
import com.ruihu.admin.module.business.tasknode.domain.dto.NodeDefectRelationDTO;
import com.ruihu.admin.module.business.tasknode.domain.dto.NodeDefectRelationUpdateDTO;
import com.ruihu.admin.module.business.tasknode.domain.entity.NodeDefectRelationEntity;
import com.ruihu.admin.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Objects;

/**
 * [  ]
 *
 * @author Tony
 * @version 1.0
 * @company 瑞虎（南京）信息技术有限公司
 * @copyright (c)  瑞虎（南京）信息技术有限公司Inc. All rights reserved.
 * @date 2022-09-22 20:56:54
 * @since JDK1.8
 */
@Service
public class NodeDefectRelationService {

    @Autowired
    private NodeDefectRelationDao nodeDefectRelationDao;

    /**
     * 根据id查询
     */
    public NodeDefectRelationEntity getById(Long id){
        return  nodeDefectRelationDao.selectById(id);
    }

    /**
     * 添加
     * @author Tony
     * @date 2022-09-22 20:56:54
     */
    public ResponseDTO<String> add(NodeDefectRelationAddDTO addDTO) {
        NodeDefectRelationEntity entity = BeanUtil.copy(addDTO, NodeDefectRelationEntity.class);
        nodeDefectRelationDao.insert(entity);
        return ResponseDTO.succ();
    }

    /**
     * 编辑
     * @author Tony
     * @date 2022-09-22 20:56:54
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> update(NodeDefectRelationUpdateDTO updateDTO) {
        NodeDefectRelationEntity entity = BeanUtil.copy(updateDTO, NodeDefectRelationEntity.class);
        nodeDefectRelationDao.updateById(entity);
        return ResponseDTO.succ();
    }

    /**
     * 删除
     * @author Tony
     * @date 2022-09-22 20:56:54
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteByIds(List<Long> idList) {
        QueryWrapper<NodeDefectRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",idList);
        nodeDefectRelationDao.delete(queryWrapper);
    }

    /**
     * 删除
     * @author Tony
     * @date 2022-09-22 20:56:54
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteByDefectType(NodeDefectRelationDTO dto) {
        QueryWrapper<NodeDefectRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("defect_type_id",dto.getDefectTypeId());
        queryWrapper.eq("task_node_id",dto.getTaskNodeId());
        nodeDefectRelationDao.delete(queryWrapper);
    }

    /**
     * 查询
     * @author Tony
     * @date 2022-09-22 20:56:54
     */
    public List<NodeDefectRelationEntity> queryByDefectType(NodeDefectRelationDTO dto) {
        QueryWrapper<NodeDefectRelationEntity> queryWrapper = new QueryWrapper<>();
        if(Objects.nonNull(dto.getDefectTypeId())){
            queryWrapper.eq("defect_type_id",dto.getDefectTypeId());
        }
        if(Objects.nonNull(dto.getTaskNodeId())){
            queryWrapper.eq("task_node_id",dto.getTaskNodeId());
        }
        return nodeDefectRelationDao.selectList(queryWrapper);
    }

}
